package com.frank.yinyuetai;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.frank.yinyuetai.Bean.VideoBean;
import com.frank.yinyuetai.Bean.YueDanDetailBean;
import com.frank.yinyuetai.Fragment.YueDanDescribeFragment;
import com.frank.yinyuetai.Fragment.YueDanListFragment;
import com.frank.yinyuetai.Util.URLProviderUtil;
import com.google.gson.Gson;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2016/8/17 0017.
 */
public class YueDanDetailActivity extends AppCompatActivity implements View.OnClickListener {
    @Bind(R.id.videoplayer)
    JCVideoPlayerStandard videoplayer;
    @Bind(R.id.yuedan_describe)
    ImageView yuedanDescribe;
    @Bind(R.id.yuedan_comment)
    ImageView yuedanComment;
    @Bind(R.id.yuedan_list)
    ImageView yuedanList;

    private YueDanDescribeFragment describeFragment;
    private YueDanListFragment yueDanListFragment;
    private int id;
    private List<VideoBean> mList;
    private YueDanDetailBean yuedanDetailBean;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yuedan_activity_layout);
        ButterKnife.bind(this);
        yuedanDescribe.setOnClickListener(this);
        yuedanComment.setOnClickListener(this);
        yuedanList.setOnClickListener(this);
        mList = new ArrayList<>();
        id = getIntent().getExtras().getInt("id");
        requestData();
    }

    private void requestData() {
        OkHttpUtils.getInstance()
                .get(URLProviderUtil.getPeopleYueDanList(id))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Gson gson = new Gson();
                        YueDanDetailBean detailBean = gson.fromJson(s, YueDanDetailBean.class);
                        List<VideoBean> vList = detailBean.getVideos();
                        Log.i("dfjkljlk", vList.get(0).getTitle());
                        describeFragment = YueDanDescribeFragment.newInstance(detailBean);
                        yueDanListFragment = YueDanListFragment.newInstance(detailBean);
                        yueDanListFragment.setPlayVideoListener(playVideoListener);
                        setFragment(describeFragment);
                        mList.addAll(vList);
                        Message message = new Message();
                        message.what = 1;
                        handler.sendMessage(message);
                    }

                });
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            videoplayer.setUp(mList.get(0).getHdUrl(), mList.get(0).getTitle());
            videoplayer.startButton.performClick();
        }
    };

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.yuedan_describe:
                setImageBackground(yuedanDescribe, R.drawable.player_yue_p);
                setImageBackground(yuedanComment, R.drawable.player_yue_comment);
                setImageBackground(yuedanList, R.drawable.player_yuelist);
                setFragment(describeFragment);
                break;
            case R.id.yuedan_comment:
                setImageBackground(yuedanDescribe, R.drawable.player_yue);
                setImageBackground(yuedanComment, R.drawable.player_yue_comment_p);
                setImageBackground(yuedanList, R.drawable.player_yuelist);
                break;
            case R.id.yuedan_list:
                setImageBackground(yuedanDescribe, R.drawable.player_yue);
                setImageBackground(yuedanComment, R.drawable.player_yue_comment);
                setImageBackground(yuedanList, R.drawable.player_yuelist_p);
                setFragment(yueDanListFragment);
                break;
        }
    }

    private void setImageBackground(ImageView imageView, int resId) {
        imageView.setBackgroundResource(resId);
    }

    private void setFragment(Fragment fragment) {
        if (fragment == null) {
            return;
        }
        FragmentManager fragmentManager = this.getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        if (fragment.isAdded() && fragment.isVisible()) {
            return;
        }
        if (fragment.isAdded()) {
            transaction.show(fragment);
        } else {
            transaction.replace(R.id.fragment_content, fragment);
        }
        transaction.commit();
    }

    @Override
    protected void onPause() {
        super.onPause();
        JCVideoPlayer.releaseAllVideos();
    }

    private PlayVideoListener playVideoListener = new PlayVideoListener() {
        @Override
        public void playVideo(String url, String title) {
            videoplayer.setUp(url, title);
            videoplayer.startButton.performClick();
        }
    };
}
